
import { customRef } from 'vue'

let _tempFunc = null;
/**
 * 函数防抖
 * @param {Function} fn 执行函数
 * @param {Number} delay 延迟执行时间
 */
export const debounce = (fn,delay) => {
  var delayTime = delay || 300;
  var timer;

  if(typeof fn !== "function") throw new TypeError("传入参数错误")

  return function(){
    var th = this , args = arguments;
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }

    timer = setTimeout(function() {
      fn.apply(th,args);
    },delayTime);
    
  }
}

export function customDebounce( value ,delay = 1000) {

  let timer ;

  customRef((track,trigger) => {
    return {

      get value(){
        track();
        return value;
      },
      set value(val) {

        clearTimeout(timer);

        timer = setTimeout(() =>{
          trigger();
          value = val;
        },delay);
      }
  
    }
  })

}